package org.real.guetshop.controller.user;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.exception.StorageNotEnoughException;
import org.real.guetshop.pojo.dto.OrderSubmitDTO;
import org.real.guetshop.pojo.vo.OrderOldVO;
import org.real.guetshop.pojo.vo.OrderSubmitVO;
import org.real.guetshop.result.Result;
import org.real.guetshop.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 用户下单
     * @param orderSubmitDTO
     * @return
     */
    @PostMapping("/sumbit")
    @Operation(summary = "用户下单")
    public Result<OrderSubmitVO> sumbit(@RequestBody OrderSubmitDTO orderSubmitDTO) throws StorageNotEnoughException {
        log.info("用户下单：{}", orderSubmitDTO);
        orderService.submit(orderSubmitDTO);
        return Result.success();
    }

    /**
     * 订单完成
     * @return
     */
    @PutMapping("/finish")
    @Operation(summary = "订单完成")
    public Result finish(@RequestParam(value = "number") String orderNumber){
        orderService.finish(orderNumber);
        return Result.success();
    }

    /**
     * 查看历史订单
     * @return
     */
    @GetMapping("/history")
    @Operation(summary = "查看历史订单")
    public Result<List<OrderOldVO>> getHistory(){
        log.info("开始查看历史订单...");
        List<OrderOldVO> list = orderService.getHistory();
        if (list != null && list.size() > 0){
            return Result.success(list);
        }else {
            return Result.error("历史订单为空");
        }
    }
}
